/**   
 * Copyright 2011 COMERE Harold
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package etrl.core;

import java.util.ArrayList;
import java.util.Iterator;

import etrl.geometry.Drawable;

/**
 * \class Geode
 * \brief Leaf of the scene graph.
 * It contains the geometry data as drawables.
 * @author harold
 *
 */
public class Geode extends Node
{
	/**
	 * List of drawables in this geode
	 */
	private ArrayList <Drawable> _drawables;
	
	/**
	 * \brief Initializes an empty geode
	 */
	public Geode()
	{
		super();
		_drawables = new ArrayList <Drawable>();
	}
		
	/**
	 * \brief Renders drawables contained in this geode
	 */
	public void render()
	{
		// For each drawable
		for(Iterator <Drawable> it = _drawables.iterator(); it.hasNext();)
		{
			// draw it ..
			it.next().drawGeometry();
		}
	}
	
	/**
	 * \brief Adds a given drawable in this geode.
	 * @param[in] drawable The drawable to add.
	 */
	public void addDrawable(Drawable drawable)
	{
		_drawables.add(drawable);
	}
	
	/**
	 * \brief Unlinks all geode's drawables
	 */
	public void removeAllDrawable()
	{
		_drawables.clear();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void accept(NodeVisitor nodeVisitor)
	{
		nodeVisitor.visit(this);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public BoundingSphere computeBoundingSphere()
	{
		// Get a new bounding sphere
		BoundingSphere bsphere = new BoundingSphere();
		
		// And extend it with the bounding sphere of each drawable
		for(Iterator <Drawable> it = _drawables.iterator(); it.hasNext();)
		{
			bsphere.expandBy(it.next().getBoundingBox());
		}
		
		return bsphere;
	}
	
	/**
	 * \brief Gets the list of drawables in this geode
	 * @return drawables list
	 */
	public ArrayList <Drawable> getDrawables()
	{
		return new ArrayList <Drawable>(_drawables);
	}
}
